package com.zl.learning.arithmetic.test;

import java.util.Arrays;
import java.util.stream.Collectors;


public class Node234 {
    private Node234 parent;
    //数据项，由小到大排列，下标为0数据最小
    private Integer[] dataItem = new Integer[3];
    //数据项数量
    private int dataNum = 0;
    //子节点，由其中的dataItem数据由小到大，下标为0最小
    private Node234[] children = new Node234[4];

    public boolean isFull() {
        return dataNum == 3;
    }

    public boolean isLeaf() {
        return children[0] == null;
    }

    public void addChild(Node234 node, int position) {
        if (position >= 4) {
            throw new RuntimeException("超过子节点新增范围");
        }
        children[position] = node;
        if (node != null) {
            node.parent = this;
        }
    }

    public void removeChild(int position) {
        if (position >= 4) {
            throw new RuntimeException("超过子节点新增范围");
        }
        children[position] = null;
    }


    public void addDataItem(int value) {
        int postion = 0;
        for (; postion < dataNum; postion++) {
            if (value < dataItem[postion]) {
                break;
            }
        }
        if (postion >= dataNum) {
            dataItem[postion] = value;
        } else {
            //平移数据
            if (dataNum - 1 + 1 - postion >= 0)
                System.arraycopy(dataItem, postion, dataItem, postion + 1, dataNum - 1 + 1 - postion);
            dataItem[postion] = value;
        }
        dataNum++;
    }

    public void removeDataItem(int value) {
        int position = 0;
        for (; position < dataNum; position++) {
            if (value == dataItem[position]) {
                break;
            }
        }
        if (position >= dataNum) {
            throw new RuntimeException("无法删除节点中的" + value);
        } else {
            //平移数据
            if (dataNum - 1 - position >= 0)
                System.arraycopy(dataItem, position + 1, dataItem, position, dataNum - 1 - position);
            dataItem[dataNum - 1] = null;
        }
        dataNum--;
    }

    public String toString() {
        return Arrays.stream(dataItem).map(String::valueOf).collect(Collectors.joining("|"));
    }

    public int getDataNum() {
        return dataNum;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Node234 node234 = (Node234) o;

        if (dataNum != node234.dataNum) return false;
        if (parent != null ? !parent.equals(node234.parent) : node234.parent != null) return false;
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
        if (!Arrays.equals(dataItem, node234.dataItem)) return false;
        // Probably incorrect - comparing Object[] arrays with Arrays.equals
        return Arrays.equals(children, node234.children);

    }

    @Override
    public int hashCode() {
        int result = parent != null ? parent.hashCode() : 0;
        result = 31 * result + Arrays.hashCode(dataItem);
        result = 31 * result + dataNum;
        result = 31 * result + Arrays.hashCode(children);
        return result;
    }
}
